/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChartSelectorDialog.java
 *
 * Created on 2011-06-04, 20:17:55
 */
package JFreeChartExtensions.ChartPanelDialogs;

import JFreeChartExtensions.Collections.DatasetCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import org.jfree.data.xy.XYIntervalSeries;

/**
 *
 * @author Nobody
 */
public final class AddRemoveSeriesDialog extends ChartPanelDialog {

    private List<JCheckBox> removeSeriesCheckboxes = new LinkedList<JCheckBox>();
    //private TreeMap<Integer, Integer> datasetOrderMapping = new TreeMap<Integer, Integer>();

    public AddRemoveSeriesDialog(ExtendableChartPanel chartPanel) {
        super(chartPanel);
        initComponents();
        fillRemoveSection();
    }

    private void fillRemoveSection() {
        //TreeMap<Integer, XYReggresionSeriesCollection> map = frameWithChart.getDatasetCollection().getDataSeriesCollections();
        DatasetCollection datasetCollection = parentPanel.getDatasetCollection();
        int i = -1;
        // for (Entry<Integer, XYReggresionSeriesCollection> entry : map.entrySet()) {
        for (XYIntervalSeries series : datasetCollection.getSeriesList()) {
            JCheckBox checkbox = new JCheckBox();
            checkbox.setText("remove : " + series.getKey());
            //       datasetOrderMapping.put(++i, series.getSlot());
            ColumnPanel.add(checkbox, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 27 * (i) + 50, -1, -1));
            removeSeriesCheckboxes.add(checkbox);
        }
        //  }
        ColumnPanel.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonPanel = new javax.swing.JPanel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        SelectDataScopePanel = new javax.swing.JPanel();
        AddSeriesButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        ColumnPanel = new javax.swing.JPanel();
        SelectAllButton = new javax.swing.JButton();
        UnselectAllButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(java.awt.Color.white);
        setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        OKButton.setText("OK");
        OKButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OKButtonMouseClicked(evt);
            }
        });
        ButtonPanel.add(OKButton);

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });
        ButtonPanel.add(CancelButton);

        SelectDataScopePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Add series"));

        AddSeriesButton.setText("Add series");
        AddSeriesButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                AddSeriesButtonMousePressed(evt);
            }
        });
        SelectDataScopePanel.add(AddSeriesButton);

        ColumnPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Remove Series"));
        ColumnPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        SelectAllButton.setText("Select All");
        SelectAllButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                SelectAllButtonMousePressed(evt);
            }
        });
        ColumnPanel.add(SelectAllButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));

        UnselectAllButton.setText("Unselect All");
        UnselectAllButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                UnselectAllButtonMousePressed(evt);
            }
        });
        ColumnPanel.add(UnselectAllButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 20, -1, -1));

        jScrollPane1.setViewportView(ColumnPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 398, Short.MAX_VALUE)
                    .addComponent(SelectDataScopePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SelectDataScopePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void OKButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OKButtonMouseClicked
        for (int i = 0; i < removeSeriesCheckboxes.size(); ++i) {
            JCheckBox checkBox = removeSeriesCheckboxes.get(i);
            if (checkBox.isSelected()) {
                String s = checkBox.getText().substring(9);
                //  int datasetNumber = datasetOrderMapping.get(i);
                parentPanel.getDatasetCollection().removeSeries(s);
//              
               /* if (chartInternalFrame.getDataSeriesCollections().get(datasetNumber).isEmpty()) {
                chartInternalFrame.getDataSeriesCollections().remove(i);
                chartInternalFrame.getChart().getXYPlot().setDataset(i, null);
                }*/
            }
        }
        this.dispose();
    }//GEN-LAST:event_OKButtonMouseClicked

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    }//GEN-LAST:event_formWindowGainedFocus

    private void UnselectAllButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UnselectAllButtonMousePressed
        selectUnselectAllCheckboxes(false);
    }//GEN-LAST:event_UnselectAllButtonMousePressed

    private void SelectAllButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectAllButtonMousePressed
        selectUnselectAllCheckboxes(true);
    }//GEN-LAST:event_SelectAllButtonMousePressed

    private void AddSeriesButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddSeriesButtonMousePressed
        parentPanel.getDataProvider().provideDataForChart(parentPanel);
        this.dispose();
    }//GEN-LAST:event_AddSeriesButtonMousePressed

    private void selectUnselectAllCheckboxes(boolean isSelected) {
        for (JCheckBox checkBox : removeSeriesCheckboxes) {
            checkBox.getModel().setSelected(isSelected);
        }
    }

    @Override
    public void dispose() {
        super.dispose();
    }

    public void showErrorPopup(String msg) {
        JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddSeriesButton;
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JButton CancelButton;
    private javax.swing.JPanel ColumnPanel;
    private javax.swing.JButton OKButton;
    private javax.swing.JButton SelectAllButton;
    private javax.swing.JPanel SelectDataScopePanel;
    private javax.swing.JButton UnselectAllButton;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
